<?php
/**
 * Created by PhpStorm.
 * User: 郑好
 * Date: 2019/3/15
 * Time: 上午08:52
 */

namespace app\common;

use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use InvalidArgumentException;

class JWT
{
    use InstanceTrait;

    private $secret = "JIJIJI_BEIBEIBEI__@server.fu51.cn^5#09&25%2019";
    private $tokenTtl = 60 * 60 * 2;

    public function checkTokenJWT($token)
    {
        try {
            $signer = new Sha256();
            if (!$token) {
                return false;
            }
            $parser = new Parser();
            $parse = $parser->parse($token);
            if ($parse->verify($signer, $this->secret) === false) {
                return false;
            } else {
                return $parse->getClaim("user_id");
            }
        } catch (InvalidArgumentException $e) {
            return false;
        }
    }

    public function newTokenJWT($userId, $userName = '')
    {
        $builder = new Builder();
        $signer = new Sha256();
        $builder->setIssuer("server.fu51.cn");
        $builder->setAudience("fu51-user-token");
        $builder->setId("fu51server", true);
        $builder->setIssuedAt(time());
        $builder->setNotBefore(time() + 5);
        $builder->setExpiration(time() + $this->tokenTtl);
        $builder->set("user_id", $userId);
        $builder->set("user_name", $userName);
        $builder->sign($signer, $this->secret);
        $token = $builder->getToken();
        $tokenStr = (string)$token;

        return $tokenStr;
    }


    private $adminSecret = "#Adf4b2aw123__@server.fu51.cn^5#09&25%2019";
    private $adminTokenTtl = 60 * 60 * 2;

    public function checkAdminTokenJWT($token)
    {
        try {
            $signer = new Sha256();
            if (!$token) {
                return false;
            }
            $parser = new Parser();
            $parse = $parser->parse($token);
            if ($parse->verify($signer, $this->adminSecret) === false) {
                return false;
            } else {
                return $parse->getClaim("admin_id");
            }
        } catch (InvalidArgumentException $e) {
            return false;
        }
    }

    public function newAdminTokenJWT($adminId)
    {
        $builder = new Builder();
        $signer = new Sha256();
        $builder->setIssuer("server.fu51.cn");
        $builder->setAudience("fu51-admin-token");
        $builder->setId("fu51server", true);
        $builder->setIssuedAt(time());
        $builder->setNotBefore(time() + 5);
        $builder->setExpiration(time() + $this->adminTokenTtl);
        $builder->set("admin_id", $adminId);
        $builder->sign($signer, $this->adminSecret);
        $token = $builder->getToken();
        $tokenStr = (string)$token;

        return $tokenStr;
    }


    private $secretEmail = "f3w^asdf1AS2Hs+@server.fu51.cn^81dhf47ds35ser";
    private $tokenEmailTtl = 60 * 30;

    public function checkEmailTokenJWT($token)
    {
        $signer = new Sha256();
        if (!$token) {
            return false;
        }
        $parser = new Parser();
        $parse = $parser->parse($token);
        if ($parse->verify($signer, $this->secretEmail) === false) {
            return false;
        } else {
            return $parse->getClaim("user_id");
        }
    }

    public function newEmailTokenJWT($userId)
    {
        $builder = new Builder();
        $signer = new Sha256();
        $builder->setIssuer("server.fu51.cn");
        $builder->setAudience("fu51-user-token");
        $builder->setId("fu51server", true);
        $builder->setIssuedAt(time());
        $builder->setNotBefore(time() + 5);
        $builder->setExpiration(time() + $this->tokenEmailTtl);
        $builder->set("user_id", $userId);
        $builder->sign($signer, $this->secretEmail);
        $token = $builder->getToken();
        $tokenStr = (string)$token;

        return $tokenStr;
    }
}
